package cn.ickck.law_paper_final.controller;

import cn.ickck.law_paper_final.model.entity.HouseLoan;
import cn.ickck.law_paper_final.model.entity.HouseLoanBatch;
import cn.ickck.law_paper_final.model.response.HouseLoanResponse;
import cn.ickck.law_paper_final.service.HouseLoanBatchService;
import cn.ickck.law_paper_final.service.HouseLoanService;
import cn.ickck.law_paper_final.utils.ExcelUtils;
import cn.ickck.law_paper_final.utils.JsonData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: ck
 * @Description:
 * @Date: Create in 19:41 2020/10/14
 */
@RestController
@RequestMapping("/api/pub/v2/house_loan")
public class HouseLoanController {
    @Autowired
    private HouseLoanService houseLoanService;

    @Autowired
    private HouseLoanBatchService houseLoanBatchService;


    @RequestMapping("selectByPage")
    public JsonData selectByPage(@Param("page")Integer page)
    {
        int count = houseLoanService.count();
        HouseLoanResponse houseLoanResponse = new HouseLoanResponse();
        houseLoanResponse.setCount(count);
        int start =count-page*20>0?count-page*20:0;
        int end = count-(page-1)*20>0?count-(page-1)*20:count;
        List<HouseLoan> houseLoans = houseLoanService.selectByPage(start,end);
        houseLoanResponse.setHouseLoans(houseLoans);
        return JsonData.buildSuccess(houseLoanResponse);
    }

    @RequestMapping("deleteById")
    public JsonData deleteById(Integer id)
    {
        houseLoanService.deleteById(id);
        return JsonData.buildSuccess("删除成功");
    }

    @Transactional
    @RequestMapping("insert")
    public JsonData insert(@RequestBody HouseLoan houseLoan)
    {

        if(houseLoan.getMortgageDate()==null)
        {
            houseLoan.setMortgageDate(new Date(200,4,20));
        }
        if(houseLoan.getDeadlineDate()==null)
        {
            houseLoan.setDeadlineDate(new Date(200,4,20));
        }
        if(houseLoan.getLoanDate()==null)
        {
            houseLoan.setLoanDate(new Date(200,4,20));
        }
        if(houseLoan.getContractDate()==null)
        {
            houseLoan.setContractDate(new Date(200,4,20));
        }
        if(houseLoan.getWeddingDate()==null)
        {
            houseLoan.setWeddingDate(new Date(200,4,20));
        }
        if(houseLoan.getBirthDate()==null)
        {
            houseLoan.setBirthDate(new Date(200,4,20));
        }
        if(houseLoan.getLoanEndDate()==null)
        {
            houseLoan.setLoanEndDate(new Date(200,4,20));
        }
        if(houseLoan.getLoanStartDate()==null)
        {
            houseLoan.setLoanStartDate(new Date(200,4,20));
        }
        if(houseLoan.getPaperSignDate()==null)
        {
            houseLoan.setPaperSignDate(new Date(200,4,20));
        }
        if(houseLoan.getCoupleBirthDate()==null)
        {
            houseLoan.setPaperSignDate(new Date(200,4,20));
        }
        HouseLoanBatch houseLoanBatch = new HouseLoanBatch();
        String key = UUID.randomUUID().toString();
        houseLoanBatch.setKey(key);
        houseLoanBatch.setName("eg:日期+导入者");
        houseLoanBatch.setDate(new Date());
        houseLoanBatchService.insert(houseLoanBatch);
        //单个插入
        houseLoan.setKey(key);
        houseLoanService.insert(houseLoan);
        return JsonData.buildSuccess("插入成功");
    }

    @RequestMapping("update")
    public JsonData update(@RequestBody HouseLoan houseLoan)
    {
        houseLoanService.update(houseLoan);
        return JsonData.buildSuccess("更新成功");
    }


    @RequestMapping("findByNameOrKey")
    public JsonData findByNameOrKey(@Param("key")String key,@Param("page")Integer page)
    {
        HouseLoanResponse houseLoanResponse = new HouseLoanResponse();
        List<HouseLoan> houseLoans = houseLoanService.selectByNameOrKey(key);
        houseLoanResponse.setCount(houseLoans.size());
        List<HouseLoan> houseLoans1 = new ArrayList<>();


        int start = houseLoans.size() - page * 20 > 0 ? houseLoans.size() - page * 20 : 0;
        int end = houseLoans.size() - (page - 1) * 20 > 0 ? houseLoans.size() - (page - 1) * 20 : houseLoans.size();


        for(int i = start;i<end;i++)
            houseLoans1.add(houseLoans.get(i));
        houseLoanResponse.setHouseLoans(houseLoans1);

        return JsonData.buildSuccess(houseLoanResponse);
    }

    @Transactional
    @RequestMapping("importExcel")
    public JsonData importExcel(@RequestParam("file") MultipartFile file) throws Exception
    {
        List<HouseLoan> houseLoans  = ExcelUtils.importExcel(file,0,1,HouseLoan.class);
        for(HouseLoan houseLoan:houseLoans)
        {
            if(houseLoan.getMortgageDate()==null)
            {
                houseLoan.setMortgageDate(new Date(200,4,20));
            }
            if(houseLoan.getDeadlineDate()==null)
            {
                houseLoan.setDeadlineDate(new Date(200,4,20));
            }
            if(houseLoan.getLoanDate()==null)
            {
                houseLoan.setLoanDate(new Date(200,4,20));
            }
            if(houseLoan.getContractDate()==null)
            {
                houseLoan.setContractDate(new Date(200,4,20));
            }
            if(houseLoan.getWeddingDate()==null)
            {
                houseLoan.setWeddingDate(new Date(200,4,20));
            }
            if(houseLoan.getBirthDate()==null)
            {
                houseLoan.setBirthDate(new Date(200,4,20));
            }
            if(houseLoan.getLoanEndDate()==null)
            {
                houseLoan.setLoanEndDate(new Date(200,4,20));
            }
            if(houseLoan.getLoanStartDate()==null)
            {
                houseLoan.setLoanStartDate(new Date(200,4,20));
            }
            if(houseLoan.getPaperSignDate()==null)
            {
                houseLoan.setPaperSignDate(new Date(200,4,20));
            }
            if(houseLoan.getCoupleBirthDate()==null)
            {
                houseLoan.setPaperSignDate(new Date(200,4,20));
            }
        }
        HouseLoanBatch houseLoanBatch = new HouseLoanBatch();
        String key = UUID.randomUUID().toString();
        houseLoanBatch.setName("eg:日期+导入者");
        houseLoanBatch.setKey(key);
        houseLoanBatch.setDate(new Date());
        houseLoanBatchService.insert(houseLoanBatch);
        for(HouseLoan houseLoan:houseLoans)
        {
            //校验，防止误插入
            //多数据校验
            if(houseLoan.getName()!=null)
            {
                    houseLoan.setKey(key);
                    houseLoanService.insert(houseLoan);

            }

        }

        return JsonData.buildSuccess("批量录入成功");
    }




}
